home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / SIGNAME.C < prev    next >
C/C++ Source or Header  |  1994-05-10  |  7KB  |  283 lines

  1. /* Convert between signal names and numbers.
  2.    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>        /* Some systems need this for <signal.h>.  */
  20. #include <signal.h>
  21.  
  22. #ifdef HAVE_CONFIG_H
  23. #if defined (emacs) || defined (CONFIG_BROKETS)
  24. /* We use <config.h> instead of "config.h" so that a compilation
  25.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  26.    (which it would do because it found this file in $srcdir).  */
  27. #include <config.h>
  28. #else
  29. #include "config.h"
  30. #endif
  31. #endif
  32.  
  33. /* Some systems do not define NSIG in <signal.h>.  */
  34. #ifndef    NSIG
  35. #ifdef    _NSIG
  36. #define    NSIG    _NSIG
  37. #else
  38. #define    NSIG    32
  39. #endif
  40. #endif
  41.  
  42. #if !__STDC__
  43. #define const
  44. #endif
  45.  
  46. #include "signame.h"
  47.  
  48. #ifndef HAVE_SYS_SIGLIST
  49. /* There is too much variation in Sys V signal numbers and names, so
  50.    we must initialize them at runtime.  */
  51.  
  52. static const char undoc[] = "unknown signal";
  53.  
  54. const char *sys_siglist[NSIG];
  55.  
  56. #else    /* HAVE_SYS_SIGLIST.  */
  57.  
  58. #ifndef SYS_SIGLIST_DECLARED
  59. extern char *sys_siglist[];
  60. #endif    /* Not SYS_SIGLIST_DECLARED.  */
  61.  
  62. #endif    /* Not HAVE_SYS_SIGLIST.  */
  63.  
  64. /* Table of abbreviations for signals.  Note:  A given number can
  65.    appear more than once with different abbreviations.  */
  66. typedef struct
  67.   {
  68.     int number;
  69.     const char *abbrev;
  70.   } num_abbrev;
  71. static num_abbrev sig_table[NSIG*2];
  72. /* Number of elements of sig_table used.  */
  73. static int sig_table_nelts = 0;
  74.  
  75. /* Enter signal number NUMBER into the tables with ABBREV and NAME.  */
  76.  
  77. static void
  78. init_sig (number, abbrev, name)
  79.      int number;
  80.      const char *abbrev;
  81.      const char *name;
  82. {
  83. #ifndef HAVE_SYS_SIGLIST
  84.   sys_siglist[number] = name;
  85. #endif
  86.   sig_table[sig_table_nelts].number = number;
  87.   sig_table[sig_table_nelts++].abbrev = abbrev;
  88. }
  89.  
  90. void
  91. signame_init ()
  92. {
  93. #ifndef HAVE_SYS_SIGLIST
  94.   int i;
  95.   /* Initialize signal names.  */
  96.   for (i = 0; i < NSIG; i++)
  97.     sys_siglist[i] = undoc;
  98. #endif /* !HAVE_SYS_SIGLIST */
  99.  
  100.   /* Initialize signal names.  */
  101. #if defined (SIGHUP)
  102.   init_sig (SIGHUP, "HUP", "Hangup");
  103. #endif
  104. #if defined (SIGINT)
  105.   init_sig (SIGINT, "INT", "Interrupt");
  106. #endif
  107. #if defined (SIGQUIT)
  108.   init_sig (SIGQUIT, "QUIT", "Quit");
  109. #endif
  110. #if defined (SIGILL)
  111.   init_sig (SIGILL, "ILL", "Illegal Instruction");
  112. #endif
  113. #if defined (SIGTRAP)
  114.   init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
  115. #endif
  116.   /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
  117.      SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't.  */
  118. #if defined (SIGABRT)
  119.   init_sig (SIGABRT, "ABRT", "Aborted");
  120. #endif
  121. #if defined (SIGIOT)
  122.   init_sig (SIGIOT, "IOT", "IOT trap");
  123. #endif
  124. #if defined (SIGEMT)
  125.   init_sig (SIGEMT, "EMT", "EMT trap");
  126. #endif
  127. #if defined (SIGFPE)
  128.   init_sig (SIGFPE, "FPE", "Floating point exception");
  129. #endif
  130. #if defined (SIGKILL)
  131.   init_sig (SIGKILL, "KILL", "Killed");
  132. #endif
  133. #if defined (SIGBUS)
  134.   init_sig (SIGBUS, "BUS", "Bus error");
  135. #endif
  136. #if defined (SIGSEGV)
  137.   init_sig (SIGSEGV, "SEGV", "Segmentation fault");
  138. #endif
  139. #if defined (SIGSYS)
  140.   init_sig (SIGSYS, "SYS", "Bad system call");
  141. #endif
  142. #if defined (SIGPIPE)
  143.   init_sig (SIGPIPE, "PIPE", "Broken pipe");
  144. #endif
  145. #if defined (SIGALRM)
  146.   init_sig (SIGALRM, "ALRM", "Alarm clock");
  147. #endif
  148. #if defined (SIGTERM)
  149.   init_sig (SIGTERM, "TERM", "Terminated");
  150. #endif
  151. #if defined (SIGUSR1)
  152.   init_sig (SIGUSR1, "USR1", "User defined signal 1");
  153. #endif
  154. #if defined (SIGUSR2)
  155.   init_sig (SIGUSR2, "USR2", "User defined signal 2");
  156. #endif
  157.   /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
  158.      is what is in POSIX.1.  */
  159. #if defined (SIGCHLD)
  160.   init_sig (SIGCHLD, "CHLD", "Child exited");
  161. #endif
  162. #if defined (SIGCLD)
  163.   init_sig (SIGCLD, "CLD", "Child exited");
  164. #endif
  165. #if defined (SIGPWR)
  166.   init_sig (SIGPWR, "PWR", "Power failure");
  167. #endif
  168. #if defined (SIGTSTP)
  169.   init_sig (SIGTSTP, "TSTP", "Stopped");
  170. #endif
  171. #if defined (SIGTTIN)
  172.   init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
  173. #endif
  174. #if defined (SIGTTOU)
  175.   init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
  176. #endif
  177. #if defined (SIGSTOP)
  178.   init_sig (SIGSTOP, "STOP", "Stopped (signal)");
  179. #endif
  180. #if defined (SIGXCPU)
  181.   init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
  182. #endif
  183. #if defined (SIGXFSZ)
  184.   init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
  185. #endif
  186. #if defined (SIGVTALRM)
  187.   init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
  188. #endif
  189. #if defined (SIGPROF)
  190.   init_sig (SIGPROF, "PROF", "Profiling timer expired");
  191. #endif
  192. #if defined (SIGWINCH)
  193.   /* "Window size changed" might be more accurate, but even if that
  194.      is all that it means now, perhaps in the future it will be
  195.      extended to cover other kinds of window changes.  */
  196.   init_sig (SIGWINCH, "WINCH", "Window changed");
  197. #endif
  198. #if defined (SIGCONT)
  199.   init_sig (SIGCONT, "CONT", "Continued");
  200. #endif
  201. #if defined (SIGURG)
  202.   init_sig (SIGURG, "URG", "Urgent I/O condition");
  203. #endif
  204. #if defined (SIGIO)
  205.   /* "I/O pending" has also been suggested.  A disadvantage is
  206.      that signal only happens when the process has
  207.      asked for it, not everytime I/O is pending.  Another disadvantage
  208.      is the confusion from giving it a different name than under Unix.  */
  209.   init_sig (SIGIO, "IO", "I/O possible");
  210. #endif
  211. #if defined (SIGWIND)
  212.   init_sig (SIGWIND, "WIND", "SIGWIND");
  213. #endif
  214. #if defined (SIGPHONE)
  215.   init_sig (SIGPHONE, "PHONE", "SIGPHONE");
  216. #endif
  217. #if defined (SIGPOLL)
  218.   init_sig (SIGPOLL, "POLL", "I/O possible");
  219. #endif
  220. #if defined (SIGLOST)
  221.   init_sig (SIGLOST, "LOST", "Resource lost");
  222. #endif
  223. #if defined (SIGDANGER)
  224.   init_sig (SIGDANGER, "DANGER", "Danger signal");
  225. #endif
  226. }
  227.  
  228. /* Return the abbreviation for signal NUMBER.  */
  229.  
  230. char *
  231. sig_abbrev (number)
  232.      int number;
  233. {
  234.   int i;
  235.  
  236.   if (sig_table_nelts == 0)
  237.     signame_init ();
  238.   
  239.   for (i = 0; i < sig_table_nelts; i++)
  240.     if (sig_table[i].number == number)
  241.       return (char *)sig_table[i].abbrev;
  242.   return NULL;
  243. }
  244.  
  245. /* Return the signal number for an ABBREV, or -1 if there is no
  246.    signal by that name.  */
  247.  
  248. int
  249. sig_number (abbrev)
  250.      const char *abbrev;
  251. {
  252.   int i;
  253.  
  254.   if (sig_table_nelts == 0)
  255.     signame_init ();
  256.  
  257.   /* Skip over "SIG" if present.  */
  258.   if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
  259.     abbrev += 3;
  260.  
  261.   for (i = 0; i < sig_table_nelts; i++)
  262.     if (abbrev[0] == sig_table[i].abbrev[0]
  263.     && strcmp (abbrev, sig_table[i].abbrev) == 0)
  264.       return sig_table[i].number;
  265.   return -1;
  266. }
  267.  
  268. #ifndef HAVE_PSIGNAL
  269. /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
  270.    a colon, and followed by a newline.  */
  271.  
  272. void
  273. psignal (signal, message)
  274.      int signal;
  275.      const char *message;
  276. {
  277.   if (signal <= 0 || signal >= NSIG)
  278.     fprintf (stderr, "%s: unknown signal", message);
  279.   else
  280.     fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
  281. }
  282. #endif
  283.